package com.qifan.alb2b.dao;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.stereotype.Repository;

import com.cqcis.base.orm.jdbc.JdbcUtil;
import com.qifan.alb2b.domain.ShoppingInfoDomain;
import com.qifan.alb2b.util.DataGridModel;
import com.qifan.alb2b.util.Page;

/**
 * 活动CURD
 * @author hc24
 *
 */
@Repository
public class ShoppingInfoDao extends BaseDao {
	private final static Log logger=LogFactory.getLog(ShoppingInfoDao.class);
	
	/**
	 * 根据条件分页查询购物信息
	 * @param dgm 分页参数
	 * @param sid 购物查询参数
	 * @return Page对象（分页信息&分页数据）
	 */
	public Page query(DataGridModel dgm,ShoppingInfoDomain sid){
		logger.debug("活动查询");
		/* 构建sql */
		StringBuilder sql=new StringBuilder();
		List<Object> params=new ArrayList<Object>();
		sql.append("select * from shoppinginfo where 1=1");
		if(sid.getSpAddr()!=null&&!"".equals(sid.getSpAddr())){//购物地点
			sql.append(" and spAddr like ?");
			params.add("%"+sid.getSpAddr()+"%");
		}
		if(sid.getPrice()!=-1){//价格小于等于
			sql.append(" and price<=?");
			params.add(sid.getPrice());
		}
		if(sid.getCityId()!=null&&!"".equals(sid.getCityId())){
			sql.append(" and cityId=?");
			params.add(sid.getCityId());
		}
		//判断是否排序
		if(dgm.getSort()!=null&&!"".equals(dgm.getSort()))
			sql.append(" order by ").append(dgm.getSort()).append(" ").append(dgm.getOrder());
		return super.queryObjByPage(sql.toString(), dgm.getRows(), dgm.getPage(), params, ShoppingInfoDomain.class);
	}
	
	public ShoppingInfoDomain getShoppingById(String sid){
		String sql="select * from shoppingInfo where spId=?";
		List<ShoppingInfoDomain> list=JdbcUtil.queryForList(this, ShoppingInfoDomain.class, sql, sid);
		if(list.size()>0)
			return list.get(0);
		return null;
	}
	
	public List<ShoppingInfoDomain> getShoppingByIds(String[] ids){
		StringBuilder sql=new StringBuilder();
		StringBuilder debugId=new StringBuilder("[");
		sql.append("select * from shoppingInfo where spId in(");
		for(int i=0;i<ids.length-1;i++){
			sql.append("?,");
			debugId.append(ids[i]).append(",");
		}
		sql.append("?)");
		debugId.append(ids[ids.length-1]).append("]");
		logger.debug("查询sql："+sql+debugId);
		return JdbcUtil.queryForList(this, ShoppingInfoDomain.class, sql.toString(), ids);
	}
	
	public void add(ShoppingInfoDomain shoppingInfoDomain){
		super.addObj(shoppingInfoDomain);
	}
	
	public void update(ShoppingInfoDomain shoppingInfoDomain){
		super.updateObj(shoppingInfoDomain, "spId");
	}
	
	public void delete(String[] ids){
		StringBuilder delSql=new StringBuilder();
		StringBuilder debugId=new StringBuilder("[");
		delSql.append("delete from shoppingInfo where spId in(");
		for(int i=0;i<ids.length-1;i++){
			delSql.append("?,");
			debugId.append(ids[i]).append(",");
		}
		delSql.append("?)");
		debugId.append(ids[ids.length-1]).append("]");
		logger.debug("删除sql："+delSql+debugId);
		this.getJdbcTemplate().update(delSql.toString(), ids);
	}
}
